package com.song.promoter.renren;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;

public class Renren {

	public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException {
		
		WebClient webClient  = new WebClient(BrowserVersion.INTERNET_EXPLORER_6);//参数是浏览器版本号 
		webClient.setJavaScriptEnabled(false);
		HtmlPage page = webClient.getPage("http://www.renren.com/");
		HtmlForm loginForm = (HtmlForm) page.getElementById("loginForm");
		
		HtmlElement nameElement = loginForm.getElementById("email");//获取输入用户名的表单
		nameElement.click();
		nameElement.setAttribute("value", "songyang315@126.com");
		
		HtmlElement passwordElement = loginForm.getElementById("password");
		passwordElement.click();
		passwordElement.setAttribute("value","3151987");
		
		HtmlSubmitInput button = (HtmlSubmitInput)loginForm.getElementById("login"); 
		System.out.println(button.getAttribute("value"));
		HtmlPage page2 = button.click();
		String resq = page2.getWebResponse().getContentAsString();
		try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		FileWriter fw1 = new FileWriter(new File("D:/renren.html"));
		fw1.write(resq);
		fw1.flush();
		fw1.close();
	}
}
